library MissionExperience requires LegacySystem

	globals
		private boolean pvt_present = false
	endglobals
	
	function GenericActions takes nothing returns nothing
		local integer i = 1
		local integer i2
		local player p

		// Generic Camera Sway
		call CameraSetTargetNoise(5.0,0.03)
		if udg_Players>=udg_ServicePlayerRequirement then
			if udg_CurrentWave==7 then
				set udg_ServiceMedalBoolean[1] = true
			elseif udg_CurrentWave==9 then
				set udg_ServiceMedalBoolean[2] = true
			elseif udg_CurrentWave==12 then
				set udg_ServiceMedalBoolean[3] = true
			elseif udg_CurrentWave==15 then
				set udg_ServiceMedalBoolean[4] = true
			endif
			if udg_CurrentWave == 7 and udg_SquadXP < 15000 and pvt_present then
				set udg_ServiceMedalBoolean[5] = true
			endif
		endif

		set i=1
		loop
			exitwhen i>udg_InitialPlayers
			set i2 = udg_Sorted[i]
			set p = ConvertedPlayer(i2)
			if GetWidgetLife(udg_Hero[i2])<=0.405 and GetPlayerSlotState(p)!=PLAYER_SLOT_STATE_LEFT then
				set udg_Experience[i2] = udg_Experience[i2] + ( I2R( GetPlayerScore(p, PLAYER_SCORE_UNITS_KILLED) - udg_MiniMarineKills[i2] ) * 0.25 )
				set udg_MiniMarineKills[i2] = GetPlayerScore(p, PLAYER_SCORE_UNITS_KILLED)
			endif
			set i=i+1
		endloop
	endfunction


	function MissionUpdate takes nothing returns nothing
		local integer i = 1
		local player p
		loop
			exitwhen i>udg_Players
			set p = ConvertedPlayer(udg_Sorted[i])
			if udg_Experience[udg_Sorted[i]]<30000 and GetLocalPlayer()==p then
				call FlashQuestDialogButton()
			endif
			set i=i+1
		endloop
	endfunction

	function SquadRatingCalc takes nothing returns nothing
		local integer i=1
		local integer outlier=0
		loop
			exitwhen i>udg_Players
			set udg_SquadXP = ((udg_SquadXP*(I2R(i)-1.0))+udg_Experience[udg_Sorted[i]])/I2R(i)
			set i=i+1
		endloop
		set i=1
		loop
			exitwhen i>udg_Players
			if udg_Experience[udg_Sorted[i]]<5000 or udg_Experience[udg_Sorted[i]]>(udg_SquadXP*2) then
				set outlier=outlier+1
			endif
			
			if udg_Experience[udg_Sorted[i]]<5000 then
				set pvt_present = true
			endif
			set i=i+1
		endloop
		if udg_SquadXP>15000 and outlier==1 then
			set udg_SquadXP=udg_SquadXP*0.7
		elseif udg_SquadXP>25000 and outlier>=2 then
			set udg_SquadXP=udg_SquadXP*0.55
		elseif outlier>0 then
			set udg_SquadXP=udg_SquadXP*0.85
		endif
	endfunction

endlibrary